package com.lunduhotel.servlets;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lunduhotel.dao.ClienteDao;
import com.lunduhotel.dao.ReservaDao;
import com.lunduhotel.modelo.Cliente;
import com.lunduhotel.modelo.Reserva;

@WebServlet("/ServletAddReserva")
public class ServletAddReserva extends HttpServlet{
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
		
		ClienteDao clienteDao = new ClienteDao();
		ReservaDao reservaDao = new ReservaDao();
		
		Reserva reserva = new Reserva();
		
		Cliente cliente = clienteDao.getCliente(request.getParameter("cpf"));
		if(cliente == null){
			request.getSession().setAttribute("msg", "CPF inválido");
			response.sendRedirect("adiciona-reserva.jsp");
			return;
		}
		reserva.setIdCliente(cliente.getId());
		reserva.setIdQuarto(Integer.parseInt(request.getParameter("quarto")));
		Calendar dataEntrada = Calendar.getInstance();
		Calendar dataSaida = Calendar.getInstance();

		try {
			dataEntrada.setTime(f.parse(request.getParameter("dataInicio")));
			dataSaida.setTime(f.parse(request.getParameter("dataInicio")));
		} catch (ParseException e) {
			e.printStackTrace();
		}

		reserva.setDataEntrada(dataEntrada);
		reserva.setDataSaida(dataSaida);
		
		
		reservaDao.criarReservas(reserva);
		
		request.getSession().setAttribute("msg", "Reserva Adicionada");
		response.sendRedirect("adiciona-reserva.jsp");
	}

}
